<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class EOAuthUserIdentity extends EOAuthComponent implements IUserIdentity {

    
    public $scope;

    
    public $key='anonymous';
    
    public $secret='anonymous';

    
    private $provider;


    private $_providerClass='EOAuthProvider';
    private $_authenticated=false;
    private $_error;
    
    
    private $userName;
    
    private $password;

    public function __construct($attributes) {

        if(is_array($attributes)) {
            if(isset($attributes['provider'])) {
                $this->setProvider($attributes['provider']);
                unset($attributes['provider']);
            }
            else
                $this->setProvider();
            
            foreach($attributes as $attr=>$value)
                $this->$attr=$value;
        }
        else return null;
    }

    public function getError(){
        return $this->_error;
    }

    public function setError($msg) {
        $this->_error=$msg;
    }

    public function getIsAuthenticated() {
        return $this->_authenticated;
    }

    public function getId() {
        return $this->provider->token->key;
    }

    public function getName() {
        return $this->provider->token->key;
    }

    public function getPersistentStates() {
    }

    public function authenticate() {

        $session=Yii::app()->session;

        if (isset($_REQUEST['oauth_token'])) {
            $oauthToken = $_REQUEST['oauth_token'];
        }
        if (isset($_REQUEST['oauth_verifier'])) {
            $oauthVerifier = $_REQUEST['oauth_verifier'];
        }

        try {

        if (!isset($oauthToken)) {
                        $consumer = new OAuthConsumer($this->key, $this->secret);

                        $scope = $this->scope;

                        $applicationName = Yii::app()->name;

                        $protocol = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
                    ? 'https://' : 'http://';
            $server = $_SERVER['HTTP_HOST'];
            $path = $_SERVER["REQUEST_URI"];
            			$callbackUrl = "";                        $token = EOAuthUtils::GetRequestToken($consumer, $scope,
                    $this->provider->request_token_endpoint, $applicationName, $callbackUrl);

                        $session['OAUTH_CONSUMER'] = $consumer;
            $session['OAUTH_TOKEN'] = $token;
            
                        $oauthVerifier = EOAuthUtils::GetVerificationCode4MiniyunWeb($token,
                    $this->provider->authorize_token_endpoint,$this->userName,$this->password);
            $token = EOAuthUtils::GetAccessToken($consumer, $token, $oauthVerifier,
                    $this->provider->access_token_endpoint);

                        $this->provider->consumer=$consumer;
            $this->provider->token=$token;
            
            $this->_authenticated = true;
        } else {
                        $consumer = $session['OAUTH_CONSUMER'];
            $token = $session['OAUTH_TOKEN'];

                        $token->key = $oauthToken;

                        $token = EOAuthUtils::GetAccessToken($consumer, $token, $oauthVerifier,
                    $this->provider->access_token_endpoint);

                        $this->provider->consumer=$consumer;
            $this->provider->token=$token;

            $this->_authenticated=true;
        }

        } catch (OAuthException $e) {
            $this->_error=$e->getMessage();
        }

        return $this->isAuthenticated;
    }

    public function setProvider($provider='EOAuthProvider') {
        if(is_string($provider))
            $this->_providerClass=$provider;
        $this->provider= new $this->_providerClass;
        if(is_array($provider))
            foreach($provider as $attr=>$val) {
                $attribute=$attr.'_token_endpoint';
                $this->provider->$attribute=$val;
            }


    }
    public function getProvider(){
        return $this->provider;
    }

}
?>
